package com.c.yinyuezhushou.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.R;

import java.util.List;

public class HomeManagerRecAdater extends RecyclerView.Adapter {
    private List<Fragment> list;
    private FragmentManager fragmentManager;

    public HomeManagerRecAdater(List<Fragment> list, FragmentManager fragmentManager) {
        this.list = list;
        this.fragmentManager = fragmentManager;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout relativeLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            relativeLayout = itemView.findViewById(R.id.fh_re);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_home,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(viewHolder.relativeLayout.getId(),list.get(position));
        fragmentTransaction.show(list.get(position));
        fragmentTransaction.commit();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
